package com.scan.bus.service;

import com.scan.bus.util.Constant;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

/**
 * Created by zhang on 2017/4/13 16 : 15.
 */

public final class InterentTools {

//    public  static String HTTPADDR = Constant.Y_HTTPADDR;//预发布接口
    public  static String HTTPADDR = Constant.Z_HTTPADDR;//正式发布接口

    private static InterentTools tools = new InterentTools();
    private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    private static OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS).retryOnConnectionFailure(true);
    private static Retrofit.Builder mRetrofitBuilder = new Retrofit.Builder();

    public Retrofit getRetrofit() {
        /*List<Interceptor> interceptors = okHttpBuilder.interceptors();
        System.out.println("拦截器数组: "+interceptors);
        if (interceptors != null && interceptors.size() > 0){
            if (interceptors.contains(null)){
                System.out.println("有空的拦截器数组: "+interceptors);
                for (int i = 0; i < interceptors.size(); i++) {
                    Interceptor interceptor = interceptors.get(i);
                    if (interceptor == null){
                        System.out.println("拦截器为空》》》》》》》》》》》》》");
                        interceptors.remove(i);
                        i--;
                    }
                }
                if (interceptors != null && interceptors.size() == 0) {
                    okHttpBuilder.addInterceptor(new HttpRequestHeader(false));
                }
            }
        }
        System.out.println("最终状态： "+okHttpBuilder.interceptors());*/

        if (mRetrofitBuilder == null)
            mRetrofitBuilder = new Retrofit.Builder();

        return mRetrofitBuilder
                .client(okHttpBuilder.build())
                .baseUrl(HTTPADDR)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
    }

    private InterentTools(){
    }

    public static InterentTools getTools(){
        if (okHttpBuilder == null)
            okHttpBuilder = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS).retryOnConnectionFailure(true);

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        List<Interceptor> interceptors = okHttpBuilder.interceptors();
        interceptors.clear();
        return tools;
    }

    /**
     * 是否开启okhttp日志
     *
     * @param flag
     * @return
     */
    public InterentTools isOpenLogging(boolean flag) {
        if (flag) {
            okHttpBuilder.addInterceptor(loggingInterceptor);
        }
        return this;
    }

    /**
     * 设置日志等级 默认body等级
     * @param level
     * @return
     */
    public InterentTools setLogLevel(boolean flag,HttpLoggingInterceptor.Level level) {
        if (flag) {
            loggingInterceptor.setLevel(level);
            okHttpBuilder.addInterceptor(loggingInterceptor);
        }
        return this;
    }

    /**
     * 设置超时时间30秒
     *
     * @return
     */
    public InterentTools connectTimeout() {
        okHttpBuilder.connectTimeout(30, TimeUnit.SECONDS);
        return this;
    }

    /**
     * 设置保存cookie
     *
     * @return
     */
    public InterentTools addReceivedCookiesInterceptor() {
        okHttpBuilder.addInterceptor(new ReceivedCookiesInterceptor());
        return this;
    }

    /**
     * 设置添加cookie
     *
     * @return
     */
    public InterentTools addCookiesInterceptor() {
        okHttpBuilder.addInterceptor(new AddCookiesInterceptor());
        return this;
    }

    public InterentTools addHeaderInterceptor(){
        okHttpBuilder.addInterceptor(new HttpRequestHeader(false));
        return this;
    }

    public InterentTools removeHeaderContentType(){
        okHttpBuilder.addNetworkInterceptor(new HttpRequestHeader(true));
        return this;
    }

    /**
     * 手动设置超时秒数
     *
     * @param timeout
     * @return
     */
    public InterentTools connectTimeout(long timeout) {
        okHttpBuilder.connectTimeout(timeout, TimeUnit.SECONDS);
        return this;
    }

    /**
     * 手动设置超时时间
     *
     * @param timeout
     * @param utils
     * @return
     */
    public InterentTools connectTimeout(long timeout, TimeUnit utils) {
        okHttpBuilder.connectTimeout(timeout, utils);
        return this;
    }

    public InterentTools addInterceptor(Interceptor interceptor) {
        okHttpBuilder.addInterceptor(interceptor);
        return this;
    }

    public InterentTools addNetworkInterceptor(Interceptor interceptor) {
        okHttpBuilder.addNetworkInterceptor(interceptor);
        return this;
    }

}
